package com.moyu.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.pagehelper.PageInfo;
import com.moyu.common.core.domain.Page;
import com.moyu.common.core.domain.R;
import com.moyu.user.domain.role.Role;
import com.moyu.user.domain.user.User;
import com.moyu.user.domain.user.dto.UserLoginDTO;
import com.moyu.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @Author 赵彦博
 * @Date 2024/11/19 10:59
 */
@RestController
@RequestMapping("users")
@Tag(name = "用户接口")
public class UserController {

    @Autowired
    private IUserService userService;

//    @PostMapping("/login")  //安全
//    @Operation(summary = "用户登录", description = "根据账号密码进行用户登录")
//    @ApiResponse(responseCode = "1000", description = "操作成功")
//    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
//    @ApiResponse(responseCode = "3102", description = "用户不存在")
//    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
//    public R<String> login(@RequestBody UserLoginDTO userLoginDTO) {
//        return userService.login(userLoginDTO.getUsername(), userLoginDTO.getPassword());
//    }

    @SaCheckRole("角色管理员")
    @PostMapping("/createUser")
    @Operation(summary = "新增用户", description = "根据信息新增用户")
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    @SaCheckRole("角色管理员")
    @PutMapping("/updateUser")
    @Operation(summary = "更新用户", description = "根据信息更新用户")
    public R<User> updateUser(@RequestBody User user) {
        User updatedUser = userService.update(user);
        return R.ok(updatedUser);
    }

    @SaCheckRole("角色管理员")
    @PostMapping("/deleteUser")
    @Operation(summary = "删除用户", description = "删除用户")
    public R deleteUser(@PathVariable Long id) {
        userService.delete(id);
        return R.ok();
    }

    @GetMapping("/query/{username}")
    @Operation(summary = "查询用户", description = "根据用户名查询用户")
    public R<User> queryUser(@PathVariable("username") String username) {
        User user = userService.query(username);
        return R.ok(user);
    }

    @GetMapping("/queryRoles/{username}")
    @Operation(summary = "查询用户角色", description = "根据用户名查询用户角色")
    public R<List<Role>> listRoleOfUser(@PathVariable String username) {
        return R.ok(userService.listRoleByUsername(username));
    }


    @PostMapping("page")
    @Operation(summary = "分页查询用户", description = "按页查询用户")
        public R<Page<User>> listUser(@RequestParam(name = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(name = "pageSize",defaultValue = "10") int pageSize) {
        PageInfo<User> pageInfo = userService.list(pageNumber, pageSize);
        return R.ok(Page.of(pageInfo.getList(), pageInfo.getTotal()));
    }

}
